<template>
  <div class="basicsConfig">
    <el-col v-bind="grid">
      <el-form :model="basicsForm" v-loading="basicsLoading" :rules="basicsRules" label-width="192px" ref="basicsRefs" class="basicsCls">
        <el-form-item prop="autoSignDay">
          <template v-slot:label>自动收货时间  <el-tooltip class="item" effect="dark" content="系统自动收货时间,单位(天数)" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
          <el-input v-limitPositiveInt v-model.number="basicsForm.autoSignDay" type="text" placeholder="请填写自动收货时间"></el-input>
        </el-form-item>
        <el-form-item prop="pendingReplenishCount">
          <template v-slot:label>待补货数量  <el-tooltip class="item" effect="dark" content="商品待补货数量低于多少时，提示补货（件数）" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
          <el-input v-limitPositiveInt v-model.number="basicsForm.pendingReplenishCount" type="text" placeholder="请填写待补货数量"></el-input>
        </el-form-item>
        <el-form-item prop="alertInventoryCount">
          <template v-slot:label>警戒库存  <el-tooltip class="item" effect="dark" content="警戒库存提醒值（件数）" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
          <el-input v-limitPositiveInt v-model.number="basicsForm.alertInventoryCount" type="text" placeholder="请填写警戒库存"></el-input>
        </el-form-item>
        <el-form-item prop="payOverTime">
          <template v-slot:label>普通商品未支付取消订单时间  <el-tooltip class="item" effect="dark" content="普通商品未支付取消订单时间，单位（分钟）" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
          <el-input v-limitPositiveInt v-model.number="basicsForm.payOverTime" type="text" placeholder="请填写未支付取消订单时间"></el-input>
        </el-form-item>
      </el-form>
    </el-col>
    <el-col :span="24">
      <el-col :span="3">
        <el-row type="flex" justify="end">
          <el-button type="primary" @click="submitAddBasics('basicsRefs')" :loading="loadingBtn">提交</el-button>
        </el-row>
      </el-col>
    </el-col>
  </div>
</template>

<script>
  import {
    getSysMallConfigApi,
    putSysMallConfigApi
  } from '@/api/setting'

  export default {
    name: "index",
    data() {
      const autoSignDayValidator = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请填写自动收货时间(天数)'));
        } else if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          callback();
        }
      };
      const pendingReplenishCountValidator = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请填写待补货(件数)'));
        } else if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          callback();
        }
      };
      const alertInventoryCountValidator = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请填写警戒库存(件数)'));
        } else if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          callback();
        }
      };
      const payOverTimeValidator = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请填写支付超时取消订单(分钟)'));
        } else if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          callback();
        }
      };
      return {
        grid: {
          xl: 8,
          lg: 9,
          md: 12,
          sm: 12,
          xs: 24
        },
        loadingBtn: false,
        basicsLoading: false,
        basicsRules: {
          autoSignDay: [
            { required: true, validator: autoSignDayValidator, trigger: 'blur' },
          ],
          pendingReplenishCount: [
            { required: true, validator: pendingReplenishCountValidator, trigger: 'blur' }
          ],
          alertInventoryCount: [
            { required: true, validator: alertInventoryCountValidator, trigger: 'blur' }
          ],
          payOverTime: [
            { required: true, validator: payOverTimeValidator, trigger: 'blur' }
          ]
        },
        basicsForm: {
          autoSignDay: null,
          pendingReplenishCount: null,
          alertInventoryCount: null,
          payOverTime: null
        }
      }
    },
    mounted() {
      this.getSysMallConfig();
    },
    methods: {
      // 获取商城系统设置
      getSysMallConfig() {
        this.$set(this, 'basicsLoading', true);
        getSysMallConfigApi().then(res => {
          this.$set(this, 'basicsForm', res);
          this.$set(this, 'basicsLoading', false);
        }).catch(() => {
          this.$set(this, 'basicsLoading', false);
        });
      },
      submitAddBasics(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$set(this, 'loadingBtn', true);
            putSysMallConfigApi(this.basicsForm).then(res => {
              this.$message.success('修改成功');
              this.getSysMallConfig();
              this.$set(this, 'loadingBtn', false);
            }).catch(() => {
              this.$set(this, 'loadingBtn', false);
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>

<style scoped>

</style>
